package com.example.opengl.egl

import android.opengl.EGL14
import android.opengl.EGLContext
import android.opengl.EGLSurface
import android.view.Surface

class EGLSurfaceHolder {
    private lateinit var mEGLCore: EGLCore
    private var mEGLSurface: EGLSurface? = null
    fun init(shareContext:EGLContext?=null,flag:Int){
        mEGLCore = EGLCore()
        mEGLCore.init(shareContext,flag)
    }
    fun createEGLSurface(surface: Surface,width:Int = -1,height:Int = -1){
        mEGLSurface = if (surface == null){
            mEGLCore.createOffscreenSurface(width,height)
        }else{
            mEGLCore.createWindowSurface(surface)
        }
    }
    fun makeCurrent(){
        mEGLSurface?.let {
            mEGLCore.makeCurrent(it)
        }
    }
    fun swapBuffers(){
        mEGLSurface?.let {
            mEGLCore.swapBuffers(it)
        }
    }

    /**
     * 设置渲染时间  单位：秒
     */
    fun setTimesTamp(timeMs:Long){
        mEGLSurface?.let {
            mEGLCore.setPresentationTime(it,timeMs*1000)
        }
    }
    fun destroySurface(){
        mEGLSurface?.let {
            mEGLCore.destroySurface(it)
        }
        mEGLSurface = EGL14.EGL_NO_SURFACE
    }
    fun release(){
        mEGLCore.release()
    }
}